//
//  PhotoView.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/18.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit
import AlamofireImage

protocol PhotoViewDelegate{
    func PhotoViewDidClick(view: PhotoView)
}

class PhotoView: UIImageView {

  
    var gifView : UIImageView!
    var delegate : PhotoViewDelegate?
    var photo : Photo!{
        didSet{
            setPhoto()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.userInteractionEnabled = true
        self.contentMode = UIViewContentMode.ScaleAspectFill
        self.clipsToBounds = true
        //self.backgroundColor = UIColor.blueColor()
        gifView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        self.addSubview(gifView)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setPhoto(){
        if photo.thumbnail_pic.absoluteString.hasSuffix(".gif"){
            gifView.hidden = false
        }else{
            gifView.hidden = true
        }
        self.af_setImageWithURL(photo.thumbnail_pic_big,placeholderImage: UIImage(named: "timeline_image_placeholder"))
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.gifView.x = self.width - self.gifView.width
        self.gifView.y = self.height - self.gifView.height
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        delegate?.PhotoViewDidClick(self)
    }
   
}
